<?php

include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/inc/usedb_inc.php");

class UserManagement {

    public function __construct() {
        include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/class/DAL/DAL.UserManagement.php");
        include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/inc/smarty_inc.php");
//include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/class/login_result_enum.php");
        $this->smarty = getSmarty();
        include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/inc/session_start.php");
    }

    public function CreateUser($user) {
        try {
            DAL_UserManagement::CreateUser($user);
            return 0;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    public function UserExist($user) {
        try {
            $r=DAL_UserManagement::UserExist($user->email);
            if ($r==null)
                return false;//user doesn't exist
            else
                return true;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    public function Login($user) {
        try {
            $user->password = md5($user->password);
            $user = DAL_UserManagement::tryLogin($user);
            if ($user != null) {
                $_SESSION['username'] = $user["username"];
                $_SESSION['userid'] = $user["id"];
                return 0;
            } else {
                return -1;
            }
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

}

?>
